<template>
  <el-dialog
    v-model="dialogVisible"
    :title="props.title"
    width="500"
    @close="handleClosed"
  >
    <el-form
      label-position="top"
      ref="formRef"
      label-width="auto"
      :model="formLabelAlign"
      style="max-width: 600px"
    >
      <el-form-item label="标题" :label-position="itemLabelPosition">
        <el-input v-model="formLabelAlign.displayName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="字段类型" :label-position="itemLabelPosition">
        <fieldSelect
          :disabled="formLabelAlign.canModify === 0"
          v-model="formLabelAlign.uiType"
        />
      </el-form-item>
      <el-form-item label="默认值" :label-position="itemLabelPosition">
        <el-input
          :disabled="formLabelAlign.canModify === 0"
          v-if="fieldType === 'text'"
          placeholder="请输入"
        />
        <el-input-number
          :disabled="formLabelAlign.canModify === 0"
          style="width: 100%"
          v-if="fieldType === 'number'"
        />
        <el-date-picker
          v-if="fieldType === 'date' || fieldType === 'datetime'"
          :type="fieldType"
          placeholder="请选择"
          style="width: 100%"
          :disabled="formLabelAlign.canModify === 0"
        />
        <div
          v-if="fieldType === 'single'"
          class="form-item"
          style="width: 100%"
        >
          <!--          <label>选项内容</label>-->
          <div class="options-container">
            <!-- 添加选项按钮 -->
            <div
              v-if="formLabelAlign.canModify !== 0"
              class="add-option-btn"
              @click="addOption"
            >
              <el-icon class="plus-icon"><Plus /></el-icon>
              <div class="plus-text">添加选项</div>
            </div>

            <!-- 选项列表 -->
            <div v-if="selectOptions.length > 0" class="options-list">
              <div
                v-for="(option, index) in selectOptions"
                :key="index"
                class="option-item"
              >
                <div class="option-content">
                  <div class="drag-icon" v-html="dragIcon"></div>

                  <!-- 始终显示输入状态 -->
                  <div class="option-edit">
                    <el-input
                      :disabled="formLabelAlign.canModify === 0"
                      type="text"
                      v-model="option.name"
                      placeholder="请输入选项"
                    />
                  </div>
                </div>
                <div
                  v-if="formLabelAlign.canModify !== 0"
                  class="option-actions"
                >
                  <button class="delete-btn" @click.stop="deleteOption(index)">
                    <el-icon><Close /></el-icon>
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
      <div class="requiredContainer">
        <div>将该字段设为必填项</div>
        <el-switch
          :disabled="formLabelAlign.canModify === 0"
          v-model="formLabelAlign.required"
        />
      </div>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div class="footerLeft">
          <el-checkbox
            v-model="formLabelAlign.disableModify"
            size="large"
            label="将该字段设为禁止可编辑"
          />
        </div>
        <div class="footerRightBtn">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave"> 保存 </el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
  import fieldSelect from "@/components/customFieldsFormItem/fieldSelect.vue";
  const dialogVisible = defineModel();
  import { computed, reactive, ref, watch } from "vue";

  import type { FormInstance, FormItemProps } from "element-plus";
  import { dragIcon } from "@/utils/SvgIcons.ts";
  import { Close, Plus } from "@element-plus/icons-vue";

  interface propsType {
    originData?: any;
    title: string;
  }
  const props = defineProps<propsType>();

  interface emitsType {
    handleSubmit: [form: any];
  }

  const emits = defineEmits<emitsType>();
  watch(
    () => props.originData,
    () => {
      if (
        typeof props.originData === "object" &&
        Object.keys(props.originData).length > 0
      ) {
        console.log("-----", props.originData);
        formLabelAlign.value = props.originData;
        if (props.originData.option) {
          selectOptions.value = JSON.parse(props.originData.option);
        }
      }
    },
  );

  const formRef = ref<FormInstance>();
  const itemLabelPosition = ref<FormItemProps["labelPosition"]>("");
  const typeMap = (type: string) => {
    let str;
    switch (type) {
      case "TEXT":
      case "TEXTAREA":
      case "LINK":
      case "ATTACHMENT":
        str = "text";
        break;
      case "SELECT":
      case "MULTIPLE":
      case "RADIO":
      case "CHECKBOX":
        str = "single";
        break;
      case "INT":
      case "DECIMAL":
        str = "number";
        break;
      case "DATE":
        str = "date";
        break;
      case "DATETIME":
        str = "datetime";
        break;
      default:
        str = "text";
    }
    if (str !== "single") {
      selectOptions.value = [];
    }
    return str;
  };
  const fieldType = computed(() => {
    return typeMap(formLabelAlign.value.uiType) as string;
  });
  const formLabelAlign = ref({
    canModify: 1,
    displayName: "",
    uiType: "",
    required: true,
    disableModify: false,
  });
  interface optionsType {
    code: string;
    name: string;
  }
  const selectOptions = ref<optionsType[]>([]);
  const addOption = () => {
    selectOptions.value.push({
      code: "",
      name: "",
    });
  };
  const deleteOption = (index: number) => {
    selectOptions.value.splice(index, 1);
  };

  const handleSave = () => {
    console.log("selectOptions", selectOptions.value, formLabelAlign);
    emits("handleSubmit", {
      ...formLabelAlign.value,
      option: selectOptions.value.filter((item) => item.name),
    });
  };

  const handleClosed = () => {
    formRef.value?.resetFields();
    formRef.value?.clearValidate();
    selectOptions.value = [];
    formLabelAlign.value = {
      canModify: 1,
      displayName: "",
      uiType: "",
      required: true,
      disableModify: false,
    };
  };
</script>
<style scoped lang="scss">
  .requiredContainer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 12px;
    background-color: #f8f8f8;
    border-radius: 4px;
  }

  .type-filter-input {
    width: 100%;
    padding: 5px 12px;
    :deep(.el-input__wrapper) {
      background-color: #f2f2f2;
      border: none;
      box-shadow: none;
    }
    :deep(.el-input__wrapper:hover) {
      box-shadow: none;
    }
    :deep(.el-input__wrapper.is-focus) {
      box-shadow: none;
    }
  }
</style>
<style lang="scss" scoped>
  .form-item {
    margin-bottom: 16px;
  }

  .form-item:last-child {
    margin-bottom: 0;
  }

  .form-item label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    color: #42464e;
  }

  .form-item input {
    width: 100%;
    padding: 7px 12px;
    border: 1px solid #dee0e3;
    border-radius: 6px;
    font-size: 14px !important;
    transition: border-color 0.3s;
    box-sizing: border-box;
    background-color: #fff !important;
    color: #1f2329 !important;
  }

  .form-item input:focus {
    outline: none;
    border-color: #1890ff;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
  }

  .form-item input[readonly] {
    background-color: #f5f6f7;
    cursor: not-allowed;
  }

  .field-type-selector {
    position: relative;
    width: 100%;
  }

  .selected-type {
    display: flex;
    align-items: center;
    padding: 6px 12px;
    border: 1px solid #dee0e3;
    border-radius: 6px;
    background-color: #fff;
    cursor: pointer;
    transition: border-color 0.3s;
  }

  .type-options::before {
    content: "";
    position: absolute;
    left: -8px;
    top: 0;
    bottom: 0;
    width: 8px;
    background: transparent;
  }

  .selected-type:hover {
    border-color: #1890ff;
  }

  .type-icon {
    width: 20px;
    text-align: center;
    margin-right: 6px;
    color: #42464e;
    line-height: 20px;
    height: 17px;
  }
  .type-cus-icon {
    margin-top: 1px;
  }

  .type-text {
    flex: 1;
    font-size: 14px;
    color: #2b2f36;
  }

  .arrow {
    color: #8f959e;
    font-size: 14px;
    transition: transform 0.3s;
    display: flex;
  }

  .type-options {
    position: absolute;
    top: 0;
    left: 100%;
    width: 160px;
    background-color: #fff;
    border: 1px solid #dee0e3;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 9999;
    max-height: 300px;
    overflow-y: auto;
    padding: 6px 0;
  }

  .field-edit-dialog {
    overflow: visible !important;
  }

  .field-edit-dialog-overlay {
    overflow: visible !important;
  }

  .type-option {
    display: flex;
    align-items: center;
    padding: 0px 8px;
    cursor: pointer;
    transition: background-color 0.3s;
    font-size: 14px;
  }
  .type-option-content {
    display: flex;
    align-items: center;
    padding: 8px 4px;
    border-radius: 4px;
    cursor: pointer;
    width: 100%;
  }
  .type-option-content:hover,
  .type-option-content.active {
    background-color: rgba(31, 35, 41, 0.1);
    color: #1890ff;
  }

  .dialog-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 16px 0;
    flex-shrink: 0; // 不缩小
  }

  .cancel-btn {
    padding: 4px 16px;
    background-color: #fff;
    color: #2b2f36;
    border: 1px solid #dee0e3;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
    width: 80px;
  }

  .cancel-btn:hover {
    color: #1890ff;
    border-color: #1890ff;
  }

  .confirm-btn {
    padding: 4px 20px;
    background-color: #1890ff;
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: background-color 0.3s;
    width: 80px;
  }

  /* 选项容器样式 */
  .options-container {
    border: 1px solid #dee0e3;
    border-radius: 6px;
    padding: 4px;
    max-height: 200px;
    overflow-y: auto;
  }

  .add-option-btn {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    color: #1890ff;
    cursor: pointer;
    border-radius: 4px;
    transition: background-color 0.3s;
  }

  .add-option-btn:hover {
    background-color: rgba(24, 144, 255, 0.1);
  }

  .plus-icon {
    margin-right: 6px;
    font-size: 14px;
  }
  .plus-text {
    font-size: 14px;
  }

  .options-list {
    margin-top: 8px;
  }

  .option-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 8px;
    margin-bottom: 4px;
    border-radius: 4px;
    transition: background-color 0.3s;
  }

  .option-item:hover {
    background-color: #f5f6f7;
  }
  .option-content {
    display: flex;
    align-items: center;
    flex: 1;
  }
  .drag-icon {
    margin-right: 6px;
    color: #8f959e;
    cursor: move;
    font-size: 14px;
    user-select: none;
    display: flex;
    align-items: center;
  }

  .option-index {
    margin-right: 8px;
    font-size: 14px;
    color: #42464e;
  }

  .arrow-icon {
    margin-right: 8px;
    color: #8f959e;
    font-size: 12px;
  }

  .option-edit {
    flex: 1;
  }

  .option-edit input {
    width: 100%;
    border-radius: 4px;
    font-size: 14px;
    box-sizing: border-box;
  }

  .option-edit input:focus {
    outline: none;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
  }

  .option-display {
    flex: 1;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: text;
    min-height: 24px;
    align-self: stretch;
    display: flex;
    align-items: center;
  }

  .option-display:hover {
    background-color: rgba(31, 35, 41, 0.05);
  }

  .option-actions {
    display: flex;
    align-items: center;
  }

  .delete-btn {
    background: none;
    border: none;
    color: #8f959e;
    cursor: pointer;
    padding: 4px;
    border-radius: 50%;
    transition:
      color 0.3s,
      background-color 0.3s;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .delete-btn:hover {
    color: #ff4d4f;
    background-color: rgba(255, 77, 79, 0.1);
  }
  .border-t {
    border-top: 1px solid rgba(31, 35, 41, 0.15);
    padding-top: 12px;
  }
  .type-options-panel {
    padding: 12px 0;
    position: fixed;
    width: 160px;
    min-height: 100px;
    background-color: #fff;
    border: 1px solid #dee0e3;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    overflow-y: auto;
    z-index: 2100;
  }
</style>
